package com.terrorism.sdk.photo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.view.View;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.terrorism.sdk.R;
import com.terrorism.sdk.utils.BitmapUtils;

/**
 * @since Monday, June 02, 2014
 * @author hoangcaomobile
 *
 */
public class PhotoLoader {

	// display image option for photo loader
	public static DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
	.showImageOnLoading(R.drawable.ic_photo_loading)
	.showImageForEmptyUri(R.drawable.ic_photo_corrupt)
	.showImageOnFail(R.drawable.ic_photo_corrupt)
	.cacheInMemory(true)
	.cacheOnDisc(true)
	.considerExifParams(true)
	.bitmapConfig(Bitmap.Config.RGB_565)
	.build();
	// --
	
	//--ducnm_start
	public static void loadPhotoByWidth(final Context activity, final String url, final ImageView iv, final int width)
	{
		ImageLoader.getInstance().loadImage(url, displayImageOptions, new SimpleImageLoadingListener() {

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				iv.setImageBitmap(ThumbnailUtils.extractThumbnail(loadedImage,width,width));
				super.onLoadingComplete(imageUri, view, loadedImage);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				
				iv.setImageBitmap(ThumbnailUtils.extractThumbnail(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_photo_corrupt),width,width));
				
				super.onLoadingFailed(imageUri, view, failReason);
			}

			@Override
			public void onLoadingStarted(String imageUri, View view) {
				iv.setImageBitmap(ThumbnailUtils.extractThumbnail(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_photo_loading),width,width));
				
				super.onLoadingStarted(imageUri, view);
			}																														
		});
	}
	//--ducnm_end

	// load photo smart
	public static void loadPhoto(final Activity activity, final String url, final ImageView iv, final int centOfWidth) {
		ImageLoader.getInstance().loadImage(url, displayImageOptions, new SimpleImageLoadingListener() {

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				iv.setImageBitmap(BitmapUtils.getBitmap(activity, loadedImage, centOfWidth));
				super.onLoadingComplete(imageUri, view, loadedImage);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				iv.setImageBitmap(BitmapUtils.getBitmapMatchScreen(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_photo_corrupt), activity));
				super.onLoadingFailed(imageUri, view, failReason);
			}

			@Override
			public void onLoadingStarted(String imageUri, View view) {
				iv.setImageBitmap(BitmapUtils.getBitmapMatchScreen(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_photo_loading), activity));
				super.onLoadingStarted(imageUri, view);
			}																														
		});
	}
	// --

	// load photo origin
	public static void loadPhotoOrigin(final String url, final ImageView iv, final Activity activity) {
		ImageLoader.getInstance().loadImage(url, displayImageOptions, new SimpleImageLoadingListener() {

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				iv.setImageBitmap(loadedImage);
				super.onLoadingComplete(imageUri, view, loadedImage);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				iv.setImageBitmap(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_photo_loading));
				super.onLoadingFailed(imageUri, view, failReason);
			}

			@Override
			public void onLoadingStarted(String imageUri, View view) {
				iv.setImageBitmap(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_photo_loading));
				super.onLoadingStarted(imageUri, view);
			}																														
		});
	}
	// --

	// load photo for status
	public static void loadPhotoMatchScreen(final String url, final ImageView iv, final Activity activity) {
		ImageLoader.getInstance().loadImage(url, displayImageOptions, new SimpleImageLoadingListener() {

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				iv.setImageBitmap(BitmapUtils.getBitmapMatchScreen(loadedImage, activity));
				super.onLoadingComplete(imageUri, view, loadedImage);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				iv.setImageBitmap(BitmapUtils.getBitmapMatchScreen(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_photo_corrupt), activity));
				super.onLoadingFailed(imageUri, view, failReason);
			}

			@Override
			public void onLoadingStarted(String imageUri, View view) {
				iv.setImageBitmap(BitmapUtils.getBitmapMatchScreen(BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_photo_loading), activity));
				super.onLoadingStarted(imageUri, view);
			}																														
		});
	}
	// --

	// load photo for wall
	public static void loadPhotoCover(final String url, final ImageView iv, final Activity activity) {
		ImageLoader.getInstance().loadImage(url, displayImageOptions, new SimpleImageLoadingListener() {

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				iv.setImageBitmap(BitmapUtils.getBitmapUserCover(loadedImage, activity));
				super.onLoadingComplete(imageUri, view, loadedImage);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				super.onLoadingFailed(imageUri, view, failReason);
			}

			@Override
			public void onLoadingStarted(String imageUri, View view) {
				super.onLoadingStarted(imageUri, view);
			}																														
		});
	}
	// --

	// load photo for avatar profile
	public static void loadAvatarProfile(final String url, final ImageView iv, final Activity activity) {
		ImageLoader.getInstance().loadImage(url, displayImageOptions, new SimpleImageLoadingListener() {

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				iv.setImageBitmap(BitmapUtils.getBitmapAvatarCircleProfile(loadedImage, activity));
				super.onLoadingComplete(imageUri, view, loadedImage);
			}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				iv.setImageBitmap(BitmapUtils.getBitmapAvatarCircleProfile(
						BitmapUtils.getBitmapAvatarCircleProfile(
								BitmapFactory.decodeResource(activity.getResources(), 
										R.drawable.ic_photo_corrupt), activity), activity));
				super.onLoadingFailed(imageUri, view, failReason);
			}

			@Override
			public void onLoadingStarted(String imageUri, View view) {
				iv.setImageBitmap(BitmapUtils.getBitmapAvatarCircleProfile(
						BitmapUtils.getBitmapAvatarCircleProfile(
								BitmapFactory.decodeResource(activity.getResources(), 
										R.drawable.ic_photo_loading), activity), activity));
				super.onLoadingStarted(imageUri, view);
			}																														
		});
	}
	// --


}
